package com.yw.smartcityforjava.adapter;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yw.smartcityforjava.App;
import com.yw.smartcityforjava.R;
import com.yw.smartcityforjava.api.news.NewsListRes;

import java.util.ArrayList;
import java.util.List;

public class NewsListAdapter extends BaseAdapter {

    private List<NewsListRes.RowsBean> datas = new ArrayList<>();

    public void setDatas(List<NewsListRes.RowsBean> datas) {
        this.datas = datas;
    }

    public List<NewsListRes.RowsBean> getDatas() {
        return datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View root;
        ViewHolder viewHolder;
        final NewsListRes.RowsBean data = datas.get(position);
        if(convertView == null){
            root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) root.findViewById(R.id.title);
            viewHolder.newsImg = (ImageView) root.findViewById(R.id.newsImg);
            viewHolder.content = (TextView) root.findViewById(R.id.content);
            viewHolder.plcount = (TextView) root.findViewById(R.id.plcount);
            viewHolder.timeTv = (TextView) root.findViewById(R.id.timeTv);
            root.setTag(viewHolder);
        }else{
            root = convertView;
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.title.setText(data.getTitle());
        Glide.with(parent.getContext()).load(App.baseUrl + data.getCover()).into(viewHolder.newsImg);
        viewHolder.content.setText(Html.fromHtml(data.getContent(), Html.FROM_HTML_MODE_COMPACT));
        viewHolder.plcount.setText("评论数: "+data.getCommentNum());
        viewHolder.timeTv.setText("发布时间: " + data.getCreateTime());


        return root;
    }


    private class ViewHolder{
        public TextView title;
        public ImageView newsImg;
        public TextView content;
        public TextView plcount;
        public TextView timeTv;
    }

}
